//
//  LGPlaceholderTextView.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/20.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGPlaceholderTextView.h"
#import <Masonry/Masonry.h>

@interface LGPlaceholderTextView ()
{
    UIEdgeInsets _insets;
}
@property(nonatomic,strong)UILabel *placeholderLab;

@property (nonatomic, readonly) NSString* realText;

@end

@implementation LGPlaceholderTextView

#pragma mark -
#pragma mark Initialisation

- (id) initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self initialUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self initialUI];
    }
    return self;
}

- (void)initialUI
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing:) name:UITextViewTextDidEndEditingNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeEditing:) name:UITextViewTextDidChangeNotification object:self];
    
    
    self.maxNum = 500;
    
    UILabel * placeholderLab =  [[UILabel alloc] init];
    placeholderLab.textColor = [UIColor lightGrayColor];
    placeholderLab.textAlignment = NSTextAlignmentLeft;
    placeholderLab.numberOfLines = 0;
    self.placeholderLab = placeholderLab;
    [self addSubview:placeholderLab];

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.placeholderLab.left = fixPlaceholderInsets(self.textContainerInset).left;
    self.placeholderLab.top = fixPlaceholderInsets(self.textContainerInset).top;
    self.placeholderLab.width = self.width - fixPlaceholderInsets(self.textContainerInset).left - fixPlaceholderInsets(self.textContainerInset).right;
    CGSize size = [self.placeholderLab sizeThatFits:CGSizeMake(self.placeholderLab.width, CGFLOAT_MIN)];
    self.placeholderLab.height = size.height;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

#pragma mark -
#pragma mark Setter/Getters

- (void)setMaxNum:(NSInteger)maxNum
{
    _maxNum = maxNum;
}


//设置PlaceHolderColor的颜色
- (void)setPlaceHolderColor:(UIColor *)placeHolderColor
{
    if (placeHolderColor !=nil)
    {
        self.placeholderLab.textColor = placeHolderColor;
    }
}

- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset
{
    [super setTextContainerInset:textContainerInset];
    [self.placeholderLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(fixPlaceholderInsets(textContainerInset));
    }];
}

- (void) setText:(NSString *)text
{
    [super setText:text];
    if (text == nil || [text isEqualToString:@""])
    {
        self.placeholderLab.hidden = NO;
    }
    else
    {
        self.placeholderLab.hidden = YES;
    }

}

- (void)setPlaceholder:(NSString *)placeholder
{
    self.placeholderLab.text = placeholder;
}

- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    self.placeholderLab.font = font;
}

- (void) beginEditing:(NSNotification*) notification
{
    if (self.text == nil)
    {
        
    }
}

// 字数限制的
- (void)changeEditing:(NSNotification*)notification
{
    UITextView *textView = (UITextView *)notification.object;
    if (textView != self)
    {
        return;
    }
    
    if (textView.text.length >= 1)
    {
        self.placeholderLab.hidden = YES;
    }
    else
    {
        self.placeholderLab.hidden = NO;
    }
    NSString *toBeString = textView.text;
    
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > self.maxNum)
        {
            //NSUInteger位置处，向后计算一个完整字符串所占据的range
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.maxNum];
            if (rangeIndex.length == 1)
            {
                textView.text = [toBeString substringToIndex:self.maxNum];
            }
            else
            {
                //range范围内完整字符串所占据的新的range。
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.maxNum)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
            //超出字符时,响应的block
            if (self.outRangeHandle)
            {
                self.outRangeHandle(self);
            }
        }
        //编辑时候响应的block
        if (self.changeEditing)
        {
            self.changeEditing(self);
        }
        
    }
}

UIEdgeInsets fixPlaceholderInsets(UIEdgeInsets insets){
    return UIEdgeInsetsMake(insets.top, insets.left + 5, insets.bottom, insets.right + 5);
}

#pragma mark -
#pragma mark Dealloc
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
